package com.wave2.shadow.validator;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.wave2.shadow.model.JoinForm;

/*EXAMPLE URLs I USED
 * http://www.vaannila.com/spring/spring-form-validation-1.html
 * http://www.vaannila.com/spring/spring-validation-using-annotation.html
 */


public class JoinFormValidator implements Validator {

	@Override
	public boolean supports(Class<?> arg0) {
		return JoinForm.class.equals(arg0);
	}

	@Override
	public void validate(Object arg0, Errors arg1) {
		ValidationUtils.rejectIfEmptyOrWhitespace(arg1, "firstName", "firstName.required");
		ValidationUtils.rejectIfEmptyOrWhitespace(arg1, "lastName", "lastName.required");
		ValidationUtils.rejectIfEmptyOrWhitespace(arg1, "email", "email.required");
		ValidationUtils.rejectIfEmptyOrWhitespace(arg1, "password", "password.required");
		
		JoinForm user = (JoinForm) arg0;
		
		if (user.getPassword() != null && user.getPassword() != "" && user.getPassword().trim().length() < 8)
		{
			arg1.rejectValue("password", "password.min.length");
		}
		ValidationUtils.rejectIfEmptyOrWhitespace(arg1, "phonenumber", "phonenumber.required");

	}

}
